package com.seaboxdata.utils;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author
 * @create 2021-01-19 12:08
 **/
public class TestRabbitMq {
    public static void main(String[] args) {

/*        realTimeBusCash.rabbitMq.ip: 210.75.252.140
        realTimeBusCash.rabbitMq.port: 50005
        realTimeBusCash.rabbitMq.username: dapeng_traffic_office
        realTimeBusCash.rabbitMq.password: dapengTrafficOffice@2020
        realTimeBusCash.rabbitMq.queueName: szt_realtime_swipe_card_data
        realTimeBusCash.kafka.realTimeBusCashTopic: realTimeBusCash
        realTimeBusCash.kafka.realTimeBusCashGroup: realTimeBusCash-group*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("210.75.252.140");
        factory.setUsername("dapeng_traffic_office");
        factory.setPassword("dapengTrafficOffice@2020");
        factory.setPort(50005);
        Connection connection = null;
        Channel channel = null;
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare("szt_realtime_swipe_card_data", BuiltinExchangeType.DIRECT);
            String queueName = "szt_realtime_swipe_card_data";
            //channel.queueDeclare(queueName, false, false, false, null);
            channel.queueBind(queueName, "szt_realtime_swipe_card_data", "sjgx");
            DefaultConsumer consumer = new DefaultConsumer(channel) {

                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String msg = new String(body, "UTF-8");
                    System.out.println(msg);
                    //channel.basicAck(envelope.getDeliveryTag(), false);

                }
            };
            channel.basicConsume(queueName, true, consumer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
